package org.youyun56.gpsserver.command;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;

import net.sf.json.JSONObject;

import org.youyun56.gpsserver.common.JSONUtils;
import org.youyun56.gpsserver.entity.Car;
import org.youyun56.gpsserver.entity.Good;
import org.youyun56.gpsserver.entity.Im;

public class CommandFactory {
    private final static String AND = "&";
    private static final String EQUALS = "=";
    public final static JSONObject INVALID_COMMAND = JSONUtils.failure("invalid command");
    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static JSONObject login(Boolean success, String msg) {
        JSONObject o = null;
        if (success) {
            o = JSONUtils.success(msg);
        } else {
            o = JSONUtils.failure(msg);
        }
        o.put("id", CommandType.Login);
        return o;
    }

    private static String getCommand(Map<String, Object> map) {
        StringBuilder sb = new StringBuilder();
        for (Entry<String, Object> entry : map.entrySet()) {
            if (sb.length() > 0) {
                sb.append(AND);
            }
            sb.append(entry.getKey());
            sb.append(EQUALS);
            sb.append(entry.getValue());
        }
        return sb.toString();
    }

    public static Object IMS(Im im) {
        JSONObject o = new JSONObject();
        o.put("id", CommandType.ADD_IM);
        o.put(JSONKey.IM, im);
        return o;
    }

    public static Object changeCarCarryStatus(Boolean output) {
        JSONObject o = new JSONObject();
        o.put("id", CommandType.Change_Car_Hearvy);
        o.put(JSONKey.SUCCESS, output);
        return o;
    }

    public static Object updateCar(Car car) {
        JSONObject o = new JSONObject();
        o.put("id", CommandType.Update_Car_Status);
        JSONObject carO = new JSONObject();
        carO.put("lastPositionTime", car.getLastPositionTime());
        carO.put("position", car.getPosition());
        carO.put("carryStatus", car.getCarryStatus());
        o.put("car", carO);
        return o;
    }

    public static JSONObject goodJson(Good good) {
        JSONObject o = new JSONObject();
        o.put("txt", good.getTxt());
        o.put("contact", good.getContact());
        o.put("city", good.getTargetCity());
        o.put("fromCity", good.getFromCity());
        o.put("toCity", good.getToCity());
        o.put("index", good.getIndex());
        o.put("hearvyType", good.getHeavyType());
        o.put("hearvyUnin", good.getHearvyUnin());
        o.put("hearvy", good.getHearvy());
        o.put("mi", good.getMi());
        o.put("carType", good.getCarType());
        o.put("addTime", sdf.format(new Date(good.getAddTime().getTime())));
        return o;
    }

    public static Object sendGood(JSONObject good) {
        JSONObject o = new JSONObject();
        o.put("id", CommandType.AddGood);
        o.put("good", good);
        return o;
    }
}
